<template>
    <Transition>
        <div class="contain" v-if="flag">
            <div class="fileBox">
                <div class="fileBox_header flex">
                    <p>批量导入</p>
                    <el-icon @click="close">
                        <Close />
                    </el-icon>
                </div>

                <div class="fileBox_main">
                    <div class="title">请选择要导入的数据文件（Excel格式）</div>

                    <div class="document" v-for="(item, index) in files" :key="index">
                        <div class="flex">
                            <img src="../../assets/images/public_excel.png">
                            <p>{{ item.name }}</p>
                        </div>
                        <img src="../../assets/images/public_delIcon.png" class="del" @click="delHandler(item.name)">
                    </div>

                    <div class="addFile" :class="{ 'addFile_active': files.length > 0 }"
                        @click="$refs.fileInput.click()">
                        <el-icon>
                            <Plus />
                        </el-icon>
                        <p>上传Excel</p>
                        <input type="file" ref="fileInput" accept=".xlsx,.xls" style="display: none;"
                            @change="handleFileSelect" />
                    </div>
                </div>

                <div class="fileBox_bottom flex">
                    <div class="downbtn" @click="exportHandle" v-loading.fullscreen.lock="fullscreenLoading"
                        v-if="templateUrl">点击查看文件上传要求/下载模板</div>
                    <div v-else> </div>
                    <div class="btnBox flex">
                        <button class="close" @click="close">取消</button>
                        <button class="agree" @click="importHandle">开始导入</button>
                    </div>
                </div>
            </div>
        </div>
    </Transition>
</template>

<script>
import { httpService } from "@/utils/httpService";
import Icon from '@/components/Icon.vue';
import { ArrowLeft, Close, Plus } from "@element-plus/icons-vue";
import { ElMessage, ElForm, ElLoading, } from "element-plus";

export default {
    name: "leftMenu",
    components: { Icon, ArrowLeft, ElMessage, ElForm, ElLoading, Close, Plus },
    data() {
        return {
            flag: false,
            files: [],
            url: "",
            templateUrl: "",
        };
    },
    computed: {},
    mounted() {

    },
    methods: {
        handleFileSelect(event) {
            // if (this.files.length > 0) {
            //     ElMessage.error("一次只能上传一个文件");
            // } else {
            const file = event.target.files[0];
            this.files.push(file)
            // }
        },
        close() {
            this.flag = false
            this.files = []
            this.url = ""
        },
        delHandler(val) {
            this.files = this.files.filter(item => item.name != val)
        },
        importHandle() {
            let params = new FormData()
            for (let i = 0; i < this.files.length; i++) {
                params.append("file", this.files[i]);
            }
            httpService(this.url, params, "post").then(
                (data) => {
                    if (data.code == 200) {
                        ElMessage.success("导入成功");
                        this.close();
                        this.$parent.reload();
                    } else {
                        ElMessage.error("导入失败，请联系管理员！");
                    }
                }
            );
        },
        exportHandle() {
            this.fullscreenLoading = true
            httpService("/javaApi/materials/exportExcel", { name: 123123123123123 }, "get").then(
                (data) => {
                    this.fullscreenLoading = false
                    if (data.code == 200) {
                        ElMessage.success("导出成功");
                        window.open(data.message, "_blank")
                    } else {
                        ElMessage.error("导出失败，请联系管理员!");
                    }
                }
            );
        },

    },
}
</script>

<style scoped>
* {
    margin: 0;
    padding: 0;
}

.flex {
    display: flex;
    align-items: center;
}

.contain {
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.35);
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 99;
    display: flex;
    align-items: center;
    justify-content: center;
}

.fileBox {
    width: 570px;
    min-height: 303px;
    background-color: #fff;
    border-radius: 5px 5px 13px 13px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.fileBox_header {
    height: 55px;
    background-color: #F2F2F2;
    justify-content: space-between;
    border: 1px solid #D6DBE2;
}

.fileBox_header p {
    margin-left: 32px;
    color: #2B2B2B;
    font-size: 18px;
}

.fileBox_header .el-icon {
    font-size: 20px;
    margin-right: 34px;
}

.fileBox_main {
    flex: 1;
    padding: 0 21px 14px;
}

.fileBox_main .title {
    color: #5B686D;
    font-size: 15px;
    padding: 21px 0 19px 0;
}

.addFile {
    width: 110px;
    height: 110px;
    border: 1px dashed #D6DBE2;
    background-color: #F5F5F5;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    color: #6F72FF;
    justify-content: center;
    align-items: center;
    font-size: 15px;
    cursor: pointer;
    transition: width 0.3s ease-in;
}

.addFile_active {
    width: 100% !important;
    height: 40px !important;
    flex-direction: row;
    transition: all 0.3s ease-in;
}

.addFile .el-icon {
    color: #6F72FF;
    margin-bottom: 10px;
    font-weight: bold;
    font-size: 18px;
}

.addFile_active .el-icon {
    margin-bottom: 0px !important;
}

.document {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: calc(100% - 28px);
    height: 48px;
    border: 1px solid #D6DBE2;
    border-radius: 10px;
    padding: 0 14px;
    margin-bottom: 14px;
    color: #000033;
    font-size: 14px;
}

.document img {
    margin-right: 11px;
}

.document .del {
    width: 17px;
    height: 17px;
}

.fileBox_bottom {
    height: 68px;
    background-color: #F2F2F2;
    justify-content: space-between;
    border: 1px solid #D6DBE2;
}

.downbtn {
    color: #6F72FF;
    font-size: 15px;
    margin-left: 21px;
    cursor: pointer;
}

.close {
    border: 1px solid #CCCCCC;
    background-color: #fff;
    border-radius: 10px;
    width: 77px;
    height: 38px;
}

.agree {
    border: none;
    background-color: #6F72FF;
    font-size: 15px;
    color: #fff;
    border-radius: 10px;
    width: 93px;
    height: 38px;
    margin: 0 34px 0 26px;
}

.v-enter-active,
.v-leave-active {
    transition: opacity 0.5s ease;
}

.v-enter-from,
.v-leave-to {
    opacity: 0;
}
</style>